package thread.demo01;

import sun.applet.Main;

/**
 * @author 真香
 * @create 2021-06-02 22:46
 */


/**
 * 多个线程同时卖票
 *
 * 发现问题：多个线程操作同一个资源的情况下，线程不安全，数据
 *
 */
public class TestThread4 implements Runnable {


    // 车票
    private int ticketNums =10;

    @Override
    public void run() {

        while (true) {

            if (ticketNums <= 0) {
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"--->拿到了第"+ticketNums--+"张票");
        }

    }


    public static void main(String[] args){

        TestThread4 t1 = new TestThread4();

        new Thread(t1,"小明").start();
        new Thread(t1,"老师").start();
        new Thread(t1,"黄牛党").start();





    }
}
